package com.sanmao.spring.demo.security.springsecurityoauth2demo.authentication.mobile;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 用户基本信息存储类
 *
 * @Author: Sanmao
 * @Date: 2021/08/31/12:08
 * @Description:
 */
public class MobileAuthenticationToken extends AbstractAuthenticationToken {

  /**
   * 用户信息全部放在这里面，如用户名，手机号，密码等
    */
  private final Object principal;
  /**
   * 这里保存的证书信息，如密码，验证码等
   */
  private Object credentials;

  public MobileAuthenticationToken(Object principal, Object credentials) {
    super(null);
    this.principal = principal;
    this.credentials = credentials;
    super.setAuthenticated(false);
  }

  public MobileAuthenticationToken(Object principal, Object credentials,
    Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    this.credentials = credentials;
    super.setAuthenticated(true);
  }

  @Override
  public void setAuthenticated(boolean authenticated) {
      if (authenticated) {
        throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
      }
      super.setAuthenticated(false);
  }

  @Override
  public Object getCredentials() {
    return this.credentials;
  }

  @Override
  public Object getPrincipal() {
    return this.principal;
  }

  @Override
  public void eraseCredentials() {
    super.eraseCredentials();
  }
}
